package dao.impl;

import dao.UserDao;
import pojo.User;
import util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    @Override
    public User login(String username, String password) {
        User user = null;
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("select * from users where username=? and password=?");
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            rs = pstmt.executeQuery();
            if (rs.next()){
                user = new User(rs.getString(1),rs.getString(2));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return user;
    }
}
